<?
include_once "DlgClassBase.class.php" ;

class DlgClass extends DlgClassBase
{
	function DlgClass($wndParent)
	{
		$this->wndParent = $wndParent ;
		$this->DlgClassBase() ;
		$this->InitWidget() ;
		
		// 上级 Package
		ASSERT( $this->aModelSelParentPackege = new GtkListStore(JCAT_Gtk_TYPE_STRING) );
		$this->wndSelParentPackege->set_model($this->aModelSelParentPackege) ;
		$aCellRenderer = new GtkCellRendererText() ;
		$this->wndSelParentPackege->pack_start($aCellRenderer) ;
		$this->wndSelParentPackege->set_attributes($aCellRenderer, 'text', 0);
		$this->wndSelParentPackege->connect_simple( 'changed', array($this,'LoadIDSel')) ;
		
		
		$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],'select * from classes where nId>0 and bClass=0 order by nId') ;
		$arrRows = sqlite_fetch_all($hRes) ;//print_r($arrRows) ;
		foreach($arrRows as $arrRow)
		{
			$s = ($arrRow['nId']>>8).':'.$arrRow['sName'] ;
			$this->aModelSelParentPackege->append(array($s)) ;
		}
		
		
		// ID 下拉选单
		ASSERT( $this->aModelSelEntID = new GtkListStore(JCAT_Gtk_TYPE_STRING) );
		$this->wndSelEntID->set_model($this->aModelSelEntID) ;
		$aCellRenderer = new GtkCellRendererText() ;
		$this->wndSelEntID->pack_start($aCellRenderer) ;
		$this->wndSelEntID->set_attributes($aCellRenderer, 'text', 0);
		$this->wndSelEntID->set_text_column(0);
		
		
		$this->wndSelType->set_active(0) ;
		$this->wndSelType->connect_simple( 'changed', array($this,'LoadIDSel')) ;
	}
	
	// 声明内部数据
	//protected $nSelParentPackege = -1 ;
//	protected $nSelType = -1 ;
	//protected $sEntID = '' ;
	//protected $sEntName = '' ;
	protected $wndParent ;
	
	// 定义按钮事件
	public function OnBtnOk()
	{
		$this->UpdateData() ;
		
		if( empty($this->sEntName) )
			return ;
			
		$nID = intval($this->sEntID) ;
		
		$sParent = $this->wndSelParentPackege->get_active_text() ;
		list($nParentID,) = explode(':',$sParent) ;
			
		if($this->nSelType==0)
		{
			$nIsClass = 1 ;
			$nID = ($nParentID<<8)|$nID ;
		}
		else
		{
			$nIsClass = 0 ;
			$nID = ($nID<<8) ;
		}
		
		$nParentID = ($nParentID<<8) ;
		
		$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],"insert into classes (nId,sName,bClass,nParentPackage) values ($nID,'{$this->sEntName}',$nIsClass,$nParentID)") ;
		
		$this->wndParent->Load() ;
		$this->destroy() ;
	}

	public function OnBntCannel()
	{ $this->destroy() ; }


	
	public function LoadIDSel()
	{
		$arrIDS = range(1,300) ;
		
		// Package
		if( $this->wndSelType->get_active() )
		{
			$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],"select * from classes where bClass=0") ;
			$arrRows = sqlite_fetch_all($hRes) ;//print_r($arrRows) ;
			foreach($arrRows as $arrRow)
			{
				$nKey = ($arrRow['nId']>>8) - 1 ;
				unset( $arrIDS[$nKey] ) ;
			}
		}
		
		// Class
		else
		{
			$sParent = $this->wndSelParentPackege->get_active_text() ;
			list($nParentID,) = explode(':',$sParent) ;
			$nParentID = $nParentID<<8 ;
			
			$hRes = sqlite_query($GLOBALS['hGlobalSqlite'],"select * from classes where bClass=1 and nParentPackage=$nParentID") ;
			$arrRows = sqlite_fetch_all($hRes) ;//print_r($arrRows) ;
			foreach($arrRows as $arrRow)
			{
				echo $nKey = ($arrRow['nId']&255) - 1 ;
				unset( $arrIDS[$nKey] ) ;
			}
		}
		
		$this->aModelSelEntID->clear() ;// print_r($arrIDS) ;
		rsort($arrIDS) ;
		foreach($arrIDS as $nID)
			$this->aModelSelEntID->append(array(strval($nID))) ;
	}
	
	private $aModelSelParentPackege ;
	private $aModelSelEntID ;

}
?>